"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DBV认证兼容性
Case Name   : 验证数据库安装所在目录中，是否存在关键文件夹/文件
Create At   : 2023/3/3
Owner       : @wan005
Description :
    1.编译并运行java文件
    2.查看关键文件夹/文件是否存在
Expect      :
    1.编译并运行java文件成功，得到执行结果
    2.关键文件夹/文件存在
History     :
"""
import os
import re
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class JdbcCertify(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.jdbc_path = os.path.join(path, 'dir_certification_0001')
        self.java_name = "jdbc_Certification_Case0001"
        self.common = Common()
        self.constant = Constant()

    def test_index(self):
        text = '-----1.编译并运行java文件-----'
        self.log.info(text)
        conf_list = {"password":self.db_primary_user_node.db_password,
                     "port":self.db_primary_user_node.db_port,
                     "hostname":self.db_primary_user_node.db_host,
                     "user":self.db_primary_user_node.db_user,
                     "dbname":self.db_primary_user_node.db_name,
                     "stringtype":"unspecified"}
        result_java = self.common.execute_jdbc(conf_list, self.jdbc_path, 
                    self.java_name, self.db_primary_root_node)

        self.assertNotIn('ERROR', result_java, "执行失败:" + text)
        self.assertEqual(1, result_java.count("数据目录为"), "执行失败:" + text)
        for i in result_java.splitlines():
            if "数据目录为" in i:
                data_dir = i.split(":")[1].strip()
                self.log.info(data_dir)

        text = '-----2.查看关键文件夹/文件是否存在-----'
        key_dir_file = ['base', 'base/1', 'global', 'pg_xlog', 'pg_tblspc',
                        'global/pg_dw_single', 'mot.conf', 'gaussdb.state']
        self.log.info(text)
        for each_dir_file in key_dir_file:
            cur_dir_file = os.path.join(data_dir, each_dir_file)
            cmd = f"ls {cur_dir_file}"
            self.log.info(cmd)
            result = self.db_primary_root_node.sh(cmd).result()
            self.assertNotIn(self.constant.no_file_msg, result, "执行失败:" + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        self.log.info(text)
        cmd = f"rm -rf {self.jdbc_path} && ls {self.jdbc_path}"
        self.log.info(cmd)
        msg = self.db_primary_root_node.sh(cmd).result()
        self.assertIn(self.constant.no_file_msg, msg, "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')